<?php

namespace app\api\controller;

use addons\recharge\library\Order;
use addons\recharge\model\MoneyLog;
use app\common\controller\Api;
use app\common\model\Recharge as RechargeModel;
use think\Exception;

/**
 * 充值
 */
class Recharge extends Api
{
    protected $noNeedLogin = ['epay','recharge'];
    protected $noNeedRight = ['*'];

    /**
     * 充值余额
     * @return string
     */
    public function recharge()
    {
        $list = RechargeModel::where('status','normal')->order('weigh desc')->select();
        $this->success('查询成功',$list);
    }

    /**
     * 余额日志
     * @return string
     */
    public function moneylog()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isPost()) {
            $where = [];
            $type = $this->request->post('type');
            $keywords = $this->request->post('keywords');
            if($keywords){
                $where['memo'] = ['like','%'.$keywords.'%'];
            }
            if($type == 1){//收入
                $where['money'] = ['>',0];
            }else if($type == 2){//支出
                $where['money'] = ['<',0];
            }

            $list = model('app\common\model\MoneyLog')
                ->where('user_id', $this->auth->id)
                ->order('createtime desc')
                ->where($where)
                ->paginate()->each(function ($item){
                    $item['type'] = $item['money'] < 0 ? '支出' : '收入';
                    $item['createtime'] = date('Y-m-d H:i:s',$item['createtime']);
                    return $item;
                });
            $this->success('ok',$list);
        }
        $this->error(__('非法请求'));
    }



    /**
     * 创建订单并发起支付请求
     */
    public function submit()
    {
        $info = get_addon_info('epay');
        if (!$info || !$info['state']) {
            $this->error('请在后台插件管理安装微信支付宝整合插件后重试');
        }
        $id = $this->request->post('id');
        $recharge = RechargeModel::get($id);
        $money = $recharge->money;
        $paytype = $this->request->post('paytype');
        if ($money <= 0) {
            $this->error('充值金额不正确');
        }

        try {
            $response = Order::submit($id, $paytype ? $paytype : 'wechat','miniapp',$this->auth->openid);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $response);
    }

    /**
     * 企业支付通知和回调
     */
    public function epay()
    {
        $type = $this->request->param('type');
        $paytype = $this->request->param('paytype');
        if ($type == 'notify') {
            $pay = \addons\epay\library\Service::checkNotify($paytype);
            if (!$pay) {
                echo '签名错误';
                return;
            }
            $data = $pay->verify();
            try {
                $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
                \addons\recharge\library\Order::settle($data['out_trade_no'], $payamount);
            } catch (Exception $e) {
            }
            return $pay->success()->send();
        } else {
            $pay = \addons\epay\library\Service::checkReturn($paytype);
            if (!$pay) {
                $this->error('签名错误');
            }
            //微信支付没有返回链接
            if ($pay === true) {
                $this->success("请返回网站查看支付状态!", "");
            }

            //你可以在这里定义你的提示信息,但切记不可在此编写逻辑
            $this->success("恭喜你！充值成功!", url("user/index"));
        }
        return;
    }
}
